package trainingcamp.day02.smallitme;
import javax.swing.JFrame;

import javax.swing.*;
import java.awt.*;

public class 交换文字顺序_02 {
    public static void main(String[] args) {

        JFrame win = new JFrame("交换文字顺序");
        win.setSize(new Dimension(500,800));
        win.setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        /*JLabel label = new JLabel("请输入要打乱的内容:");
        Font font = new Font("黑体加粗",Font.BOLD,20);
        label.setFont(font);*/
        JTextArea textArea1 = new JTextArea();
        textArea1.setPreferredSize(new Dimension(200,400));
        textArea1.setLineWrap(true);
        //textField.setSize(100,20);
        JButton btn01 = new JButton("提交");
        btn01.setSize(50,20);
        JTextArea textArea2 = new JTextArea();
        textArea2.setPreferredSize(new Dimension(200,400));
        textArea2.setLineWrap(true);
        btn01.addActionListener(e -> {

            String str = textArea1.getText();
            char[] newstr = new char[str.length()];
            char min = '\u4E00';
            char max = '\u9FA5';
            for(int i = 1; i<str.length();i+=2){
                char berfor = str.charAt(i-1);
                char after = str.charAt(i);
                if((min<=berfor && berfor<=max) && (min<=after && after<=max)){
                    newstr[i-1] = after;
                    newstr[i] = berfor;
                }else {
                    newstr[i-1] = berfor;
                    newstr[i] = after;
                }
                if(str.length()%2 != 0){
                    newstr[newstr.length-1] = str.charAt(str.length()-1);
                }
            }
            textArea2.setText(new String(newstr));
        });




//        panel.add(label);
        panel.add(textArea1);
        panel.add(btn01);
        panel.add(textArea2);

        win.add(panel);
        win.setVisible(true);
        win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
